//
//  QWNetworkRequest.h
//  Pods
//
//  Created by ZhangYanhua on 15/12/9.
//
//

#import <Foundation/Foundation.h>
#import <AFNetWorking/AFNetworking.h>
#import "QWNetworkAgentDataSource.h"
#import "QWNetworkAgentDelegate.h"

@class QWNetworkRequest;
typedef void (^QWNetworkSuccessBlock)(QWNetworkRequest *request);
typedef void (^QWNetworkFailureBlock)(QWNetworkRequest *request);

@interface QWNetworkRequest : NSObject <QWNetworkAgentDelegate, QWNetworkAgentDataSource>

/// Tag
@property (nonatomic) NSInteger tag;

/// User info
@property (nonatomic, strong) NSDictionary *userInfo;

@property (nonatomic, strong) NSURLSessionTask *sessionTask;

@property (nonatomic, strong) NSError *lastError;

@property (nonatomic, copy) QWNetworkSuccessBlock successCompletionBlock;

@property (nonatomic, copy) QWNetworkFailureBlock failureCompletionBlock;

/**
 *  获取缓存的JSON数据，对于比较小的数据可以直接主线程获取，主线程反序列化。
 *  对于比较大的数据可能需要背景线程去处理
 *
 *  @return 返回缓存的JSON数据，如果没有缓存返回nil
 */
- (id)getCachedJson;

- (void)start;
- (void)stop;

- (BOOL)isExecuting;

- (void)startWithCompletionBlockWithSuccess:(QWNetworkSuccessBlock)success
                                    failure:(QWNetworkFailureBlock)failure;

- (void)setCompletionBlockWithSuccess:(QWNetworkSuccessBlock)success
                              failure:(QWNetworkFailureBlock)failure;

- (void)clearCompletionBlock;

// 配置缓存参数, 子类可覆盖
/// 返回缓存key需要的参数
- (id)cacheKeyFilterForRequestArgument:(id)argument;

/// 缓存过期时间, 0表示不缓存
- (NSTimeInterval)cacheTimeOutSeconds;

@end